//
//  TTLeftFiltrateView.m
//  TaoTuo
//
//  Created by LG on 2020/10/28.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTLeftFiltrateView.h"
#import "TTZuanshiRightView.h"

@interface TTLeftFiltrateView ()

@property (nonatomic, strong) UIButton *backBtn;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *searchBtn;

@end

@implementation TTLeftFiltrateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchBtnClick) name:@"dismissActionNotify" object:nil];
        
        /// 钻饰参数发生变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(zuanshiParamsChangedNotify:) name:@"zuanshi_chooseItemsChangedNotify_key" object:nil];
        
        [self setupSubviews];
        
        
    }
    return self;
}

/// 钻石参数发生变化
- (void)zuanshiParamsChangedNotify:(NSNotification *)notify
{
    NSDictionary *dic = notify.object;

    [self getGoodsTotalCountWithParams:dic];
}

- (void)getGoodsTotalCountWithParams:(NSDictionary *)param
{
    NSMutableDictionary *dic = param.mutableCopy;
    [dic setObject:@(1) forKey:@"param.pageIndex"];
    [dic setObject:@(1) forKey:@"param.pageSize"];
    if (dic[@"param.categoryName"] == nil) {
        dic[@"param.categoryName"] = @"女戒";
    }
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postUrlString:queryList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        NSString *title = [NSString stringWithFormat:@"搜索(%@款)", responseObject[@"TotalCount"]];
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:title];
        [attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:[attr.string rangeOfString:@"搜索"]];
        [self.searchBtn setAttributedTitle:attr forState:UIControlStateNormal];
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    } showLoadingInView:nil];
    
}



- (void)backAction
{
    [LLAnimationAlertView dismissView:self];
}

- (void)setSelectItemsWithParamDic:(NSDictionary *)paramsDic
{
    [self.rightView setSelectItemsWithParamDic:paramsDic];
}

- (void)resetBtnClick
{
    [self.rightView resetViews];
}

// 点击搜索
- (void)searchBtnClick
{
    [LLAnimationAlertView dismissView:self];
    NSDictionary *dic = [self.rightView getParamsDic];
    if (self.clickSearchBlock) {
        
        NSString *name = [self handleSearchNameWithDic:self.rightView.searchNameDic];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"searchNameChangedNotify_key" object:name];
        
        self.clickSearchBlock(dic);
    }
}

- (NSString *)handleSearchNameWithDic:(NSDictionary *)searchDic
{
    NSString *searchName = @"";
    NSArray *keys = [searchDic.allKeys sortedArrayUsingComparator:^NSComparisonResult(NSString * _Nonnull obj1, NSString *  _Nonnull obj2) {
        return obj1.intValue > obj2.intValue;
    }];
    for (NSString *key in keys) {
        NSString *value = searchDic[key];
        if (value.length > 0) {
            searchName = [searchName stringByAppendingString:[NSString stringWithFormat:@"%@ ", searchDic[key]]];
        }
    }
    return searchName;
}

- (void)setupSubviews
{
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.fillColor = UIColor.whiteColor.CGColor;
    layer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:CGSizeMake(10, 10)].CGPath;
    [self.layer addSublayer:layer];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, StatusBarAndNavBar_Height)];
    bgView.backgroundColor = UIColor.whiteColor;
    bgView.layer.cornerRadius = 10;
    [self addSubview:bgView];
    
    _backBtn = [UIButton createButtonWithImage:[UIImage imageNamed:@"nav_back"] target:self action:@selector(backAction)];
    _backBtn.frame = CGRectMake(0, kStatusBarHeight, 44, 44);
    [bgView addSubview:_backBtn];
    
    _titleLabel = [UILabel labelTextCenterWithTitle:@"筛选" color:UIColorHex(333333) fontSize:16];
    _titleLabel.frame = CGRectMake(0, kStatusBarHeight, self.width, 44);
    [bgView addSubview:_titleLabel];
    
    [LLLineUtilies addLineInView:self startPoint:CGPointMake(0, self.backBtn.bottom) endPoint:CGPointMake(self.width, self.backBtn.bottom) lineColor:UIColorHex(f2f2f2) lineWidth:0.5];
    
    self.rightView = [[TTZuanshiRightView alloc] initWithFrame:CGRectMake(0, self.backBtn.bottom - 40, self.width, self.height - self.backBtn.bottom - 22)];
    self.rightView.backgroundColor = UIColor.clearColor;
    [self insertSubview:self.rightView belowSubview:bgView];
    
    
    UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.rightView.bottom, self.width, 62)];
    [self addSubview:bottomView];
    [LLLineUtilies addLineInView:self startPoint:CGPointZero endPoint:CGPointMake(self.width, 0) lineColor:UIColorHex(f2f2f2) lineWidth:0.5];
    
    UIButton *resetBtn = [UIButton createButtonWithTitle:@"重置" titleColor:UIColorHex(#333333) titleFontSize:16 borderColor:UIColorHex(#808080) cornerRadius:20 contentEdgeInsets:UIEdgeInsetsZero target:self action:@selector(resetBtnClick)];
    [bottomView addSubview:resetBtn];
    [resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(88);
        make.centerY.equalTo(bottomView);
    }];
    
    UIButton *searchBtn = [UIButton createButtonWithTitle:@"" titleColor:UIColor.whiteColor titleFontSize:14 borderColor:UIColorHex(#808080) cornerRadius:20 contentEdgeInsets:UIEdgeInsetsZero target:self action:@selector(searchBtnClick)];
    searchBtn.backgroundColor = UIColorHex(#8B57E6);
    [bottomView addSubview:searchBtn];
    self.searchBtn = searchBtn;
    [searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.left.equalTo(resetBtn.mas_right).offset(15);
        make.height.mas_equalTo(40);
        make.centerY.equalTo(bottomView);
    }];
    
    
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
